package cn.qingyun.gis.modules.scenes.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.qingyun.gis.modules.scenes.entity.PoiChannelModel;
import cn.qingyun.gis.modules.scenes.entity.PoiScenario;
import cn.qingyun.gis.modules.scenes.entity.PoiScenarioChannel;
import cn.qingyun.gis.modules.scenes.mapper.PoiScenarioChannelMapper;
import cn.qingyun.gis.modules.scenes.mapper.PoiScenarioMapper;
import cn.qingyun.gis.modules.scenes.request.PoiListRequest;
import cn.qingyun.gis.modules.scenes.request.PoiNumberInfoRequest;
import cn.qingyun.gis.modules.scenes.request.QueryByWordRequest;
import cn.qingyun.gis.modules.scenes.response.PoiListResponse;
import cn.qingyun.gis.modules.scenes.response.PoiNumberInfoResponse;
import cn.qingyun.gis.modules.scenes.response.UnFollowPoiDto;
import cn.qingyun.gis.modules.scenes.service.IPoiScenarioChannelService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author guest
 * @since 2024-06-13
 */
@DS("scenes_offensive")
@Service
public class PoiScenarioChannelServiceImpl extends ServiceImpl<PoiScenarioChannelMapper, PoiScenarioChannel> implements IPoiScenarioChannelService {
    @Resource
    private PoiScenarioMapper poiScenarioMapper;

    @Override
    public IPage<PoiListResponse> selectPagePoiByPhone(IPage<PoiScenario> poiIPage, PoiListRequest request) {
        return this.baseMapper.selectPagePoiByPhone(poiIPage, request);
    }

    @Override
    public List<PoiListResponse> selectPoiByPhone(QueryByWordRequest request) {
        return this.baseMapper.selectPoiByPhone(request);
    }

    @Override
    public int selectCountByPhone(String phone, String channelCode) {
        return this.baseMapper.selectCountByPhone(phone, channelCode);
    }

    @Override
    public int selectCountByTelNum(String phone, Integer type, String table) {
        return this.baseMapper.selectCountByTelNum(phone, type, table);
    }

    /**
     * poi查询
     *
     * @param poiIPage
     * @param request
     * @param table
     * @return
     */
    @Override
    public IPage<PoiListResponse> selectPagePoiCommon(IPage<?> poiIPage, PoiListRequest request, String table) {
        return this.baseMapper.selectPagePoiCommon(poiIPage, request, table);
    }

    @Override
    public IPage<PoiListResponse> selectPagePoiCommonNew(IPage<?> poiIPage, PoiListRequest request, String table, Integer leve) {
        return this.baseMapper.selectPagePoiCommonNew(poiIPage, request, table, leve);
    }

    /**
     * 关键字搜索接口
     *
     * @param request
     * @param table
     * @return
     */
    @Override
    public List<PoiListResponse> selectPoiLikeCommon(QueryByWordRequest request, String table) {
        return this.baseMapper.selectPoiLikeCommon(request, table, request.getType(), request.getType().contains("楼宇") ?
                0 : 1);
    }

    /**
     * 关键字搜索接口--改造
     *
     * @param request
     * @param table
     * @return
     */
    @Override
    public List<PoiListResponse> selectPoiLikeCommonNew(QueryByWordRequest request, String table, Integer level) {
        return this.baseMapper.selectPoiLikeCommonNew(request, table, request.getType(), request.getType().contains("商务楼宇") ?
                0 : 1, level);
    }

    @Override
    public int selectCountByChannelCode(String table, String channelCode) {
        return this.baseMapper.selectCountByChannelCode(table, channelCode);
    }

    /**
     * 责任人插入或更新
     *
     * @param list
     * @param table
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertOrUpdateByPoiId(List<?> list, String table) {
        return this.baseMapper.insertOrUpdateByPoiId(table, list);
    }

    @Override
    public int selectPoiNumberInfoCount(PoiNumberInfoRequest poiNumberInfoRequest, List<String> tables, Integer level) {
        return this.baseMapper.selectCountPoiNumberInfo(poiNumberInfoRequest, tables, level);
    }

    @Override
    public int selectPoiNumberInfoCountSub(PoiNumberInfoRequest poiNumberInfoRequest, String table, Integer level) {
        return this.baseMapper.selectCountPoiNumberInfoSub(poiNumberInfoRequest, table, level);
    }

    @Override
    public List<PoiNumberInfoResponse> selectPoiNumberInfoPage(PoiNumberInfoRequest poiNumberInfoRequest, List<String> tables, Integer level) {
        return this.baseMapper.selectPoiNumberInfoPage(poiNumberInfoRequest, tables, level);
    }

    @Override
    public List<PoiNumberInfoResponse> selectPoiNumberInfoPageSub(PoiNumberInfoRequest poiNumberInfoRequest, List<String> tables, Integer level) {
        return this.baseMapper.selectPoiNumberInfoPageSub(poiNumberInfoRequest, tables, level);
    }

    @Override
    public int selectCount(List<String> list) {
        QueryWrapper<PoiScenarioChannel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("poi_id", list);
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public List<String> hasExistPoiId(List<String> poiIdList) {
        // 使用LambdaQueryWrapper构建查询
        LambdaQueryWrapper<PoiScenario> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PoiScenario::getPoiId, poiIdList).select(PoiScenario::getPoiId);

        // 获取所有存在的poi_id
        List<String> existingPoiIds = poiScenarioMapper.selectList(queryWrapper).stream()
                .map(PoiScenario::getPoiId).collect(Collectors.toList());

        // 找出不存在的poi_id
        return poiIdList.stream()
                .filter(poiId -> !existingPoiIds.contains(poiId))
                .collect(Collectors.toList());
    }

    @Override
    public UnFollowPoiDto selectGridCode(String channelCode, String zxNumber, String zyNumber, String table) {
        return poiScenarioMapper.selectGridCode(channelCode, zxNumber, zyNumber, table);
    }

    @Override
    public List<UnFollowPoiDto> selectGridCodeList(String channelCode, String zxNumber, String zyNumber, String table) {
        return poiScenarioMapper.selectGridCodeList(channelCode, zxNumber, zyNumber, table);
    }

    @Override
    public List<String> isBuildPoiId(List<PoiChannelModel> poiChannelModels) {
        List<String> poiIdList = poiChannelModels.stream().map(PoiChannelModel::getPoiId).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(poiIdList)) {
            return poiScenarioMapper.istBuildPoi(poiIdList);
        }
        return Lists.newArrayList();
    }

    @Override
    public List<PoiChannelModel> hasExistBuildPoiId(List<PoiChannelModel> list) {
        //建筑poi_id不是全空
        List<PoiChannelModel> listFirst = list.stream().filter(entity -> !CharSequenceUtil.isEmpty(entity.getBelongBuildId())).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(listFirst)) {
            List<PoiChannelModel> poiChannelModels = poiScenarioMapper.hasExistBuildPoiId(listFirst);
            List<PoiChannelModel> collect = list.stream()
                    .filter(entity -> !poiChannelModels.stream().map(PoiChannelModel::getPoiId).collect(Collectors.toList()).contains(entity.getPoiId()))
                    .collect(Collectors.toList());
            //把belong_build_id为空的筛选出来
            List<PoiChannelModel> listSecond = list.stream().filter(entity -> CharSequenceUtil.isEmpty(entity.getBelongBuildId())).collect(Collectors.toList());
            if (CollUtil.isEmpty(listSecond)) {
                return collect;
            }
            List<PoiChannelModel> poiChannelModelPro = poiScenarioMapper.hasExistBuildPoiIdPro(listSecond);
            return collect.stream()
                    .filter(entity -> !poiChannelModelPro.stream().map(PoiChannelModel::getPoiId).collect(Collectors.toList()).contains(entity.getPoiId()))
                    .collect(Collectors.toList());
            //建筑poi_id全空
        } else {
            if (CollUtil.isEmpty(list)) {
                return list;
            }
            List<PoiChannelModel> poiChannelModelPro = poiScenarioMapper.hasExistBuildPoiIdPro(list);
            return list.stream()
                    .filter(entity -> !poiChannelModelPro.stream().map(PoiChannelModel::getPoiId).collect(Collectors.toList()).contains(entity.getPoiId()))
                    .collect(Collectors.toList());
        }
    }
}
